//package tasko.storage;
import java.util.Vector;


public class FileOperations {
	FileReadWrite fileReadWrite;
	
	public FileOperations() {
		fileReadWrite = new FileReadWrite();
	}
	public void writeList (Vector<Task> taskList) {
		fileReadWrite.writeAllTasks(TaskoConstants.FILE_NAME,convertToCSV(taskList));
	}

	private Vector<String> convertToCSV(Vector<Task> taskList) {
		Vector<String> taskListString = new Vector<String>();
		for (Task task : taskList) {
			String temp = "";
			for (String item : task.getTask()) {
				temp += ("\""+item+"\""+",");
			}
			temp = temp.substring(temp.length()-1) + "\n";
			taskListString.add(temp);
		}
		return taskListString;
	}
	
	public Vector<Task> getTasksFromFile(String fileName) {
		Vector<Task> tasks = new Vector<Task>();
		tasks.add(splitTasks(fileReadWrite.readAll(fileName)));
		return tasks;
	}
	
	private Task splitTasks(Vector<String> taskList) {
		Task task = new Task();
		for (String taskString : taskList) {
			String[] taskItem = taskString.split(",");
			task.setTaskId(Integer.parseInt(taskItem[0]));
			task.setTaskTitle(taskItem[1]);
			task.setTaskStartDate(taskItem[2]);
			task.setTaskEndDate(taskItem[3]);
			task.setTaskDescription(taskItem[4]);
			task.setIsTaskStarred(Boolean.parseBoolean(taskItem[5]));
			task.setTaskType(taskItem[6]);
			task.setIsTaskDone(Boolean.parseBoolean(taskItem[7]));
			task.setTaskSearchPriority(Integer.parseInt(taskItem[8]));
		}
		return task;
	}
	
}
